package com.geeklei.media.ui.menu.popup;

import android.animation.Animator;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.geeklei.media.ui.R;
import com.geeklei.media.ui.common.AnimatorFactory;
import com.geeklei.media.ui.common.Scheduler;

public class BaseMenuPopup extends RelativeLayout {
    private Animator mAnimator;
    private Runnable mAutoDismissRunner = new Runnable() {
        public void run() {
            BaseMenuPopup.this.dismiss();
        }
    };
    protected LinearLayout mContentView;
    private OnClickListener mDismissClick = new OnClickListener() {
        public void onClick(View view) {
            if (view == BaseMenuPopup.this) {
                BaseMenuPopup.this.dismiss();
            }
        }
    };
    private boolean mIsShowing = false;
    protected ListView mListView;
    private ImageView mMaskView;
    private OnShowHideListener<BaseMenuPopup> mShowHideListener;
    private View mTitleLayout;
    private TextView mTitleText;

    public BaseMenuPopup(Context context) {
        super(context);
        init();
    }

    private void init() {
        this.mMaskView = new ImageView(getContext());
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.addRule(ALIGN_PARENT_RIGHT);
        this.mMaskView.setImageResource(R.drawable.play_mask_right_s);
        this.mMaskView.setScaleType(ScaleType.FIT_XY);
        this.mMaskView.setLayoutParams(layoutParams);
        addView(this.mMaskView);
        int popupWidth = getPopupWidth();
        this.mContentView = new LinearLayout(getContext());
        LayoutParams layoutParams2 = new LayoutParams(popupWidth, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams2.addRule(ALIGN_PARENT_RIGHT);
        this.mContentView.setLayoutParams(layoutParams2);
        this.mContentView.setGravity(Gravity.CENTER_VERTICAL);
        this.mContentView.setOrientation(LinearLayout.VERTICAL);
        addView(this.mContentView);
        this.mTitleLayout = LayoutInflater.from(getContext()).inflate(R.layout.vp_popup_base_title, mContentView, false);
        this.mContentView.addView(this.mTitleLayout);
        this.mTitleText = (TextView) findViewById(R.id.vp_popup_base_title_name);
        this.mListView = new ListView(getContext());
        this.mListView.setDividerHeight(1);
        this.mListView.setDivider(getResources().getDrawable(R.drawable.vp_divider_bg_30));
        LinearLayout.LayoutParams layoutParams3 = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams3.rightMargin = getResources().getDimensionPixelSize(R.dimen.vp_menu_popup_list_margin_right);
        this.mListView.setLayoutParams(layoutParams3);
        this.mContentView.addView(this.mListView);
        setOnClickListener(this.mDismissClick);
    }

    protected int getPopupWidth() {
        return getContext().getResources().getDimensionPixelSize(R.dimen.vp_base_popup_width);
    }

    private void adjustPopupWidth() {
        ViewGroup.LayoutParams layoutParams = this.mContentView.getLayoutParams();
        if (layoutParams != null) {
            layoutParams.width = getPopupWidth();
            this.mContentView.setLayoutParams(layoutParams);
        }
    }

    public void setTitle(CharSequence charSequence) {
        this.mTitleText.setText(charSequence);
    }

    public void show(ViewGroup viewGroup) {
        if (!isShowing()) {
            adjustPopupWidth();
            clearAnimator();
            remove();
            viewGroup.addView(this, generateParams());
            animateIn();
            this.mIsShowing = true;
            onShowStateChanged();
            triggerAutoDismiss();
        }
    }

    public void dismiss() {
        if (isShowing()) {
            clearAnimator();
            animateOut();
            this.mIsShowing = false;
            onShowStateChanged();
            clearAutoDismiss();
        }
    }

    private void onShowStateChanged() {
        if (this.mShowHideListener == null) {
            return;
        }
        if (this.mIsShowing) {
            this.mShowHideListener.onShow(this);
        } else {
            this.mShowHideListener.onHide(this);
        }
    }

    protected void onVisibilityChanged(View view, int i) {
        super.onVisibilityChanged(view, i);
        if (view != this) {
            return;
        }
        if (i != VISIBLE) {
            if (this.mIsShowing) {
                this.mIsShowing = false;
                onShowStateChanged();
            }
            remove();
        } else if (!this.mIsShowing) {
            this.mIsShowing = true;
            onShowStateChanged();
        }
    }

    public boolean isShowing() {
        return this.mIsShowing;
    }

    private void remove() {
        if (getParent() instanceof ViewGroup) {
            ((ViewGroup) getParent()).removeView(this);
        }
    }

    private LayoutParams generateParams() {
        return new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }

    private void clearAnimator() {
        if (this.mAnimator != null) {
            this.mAnimator.end();
        }
    }

    private void animateIn() {
        this.mAnimator = AnimatorFactory.animateInRightView(this);
    }

    private void animateOut() {
        this.mAnimator = AnimatorFactory.animateOutRightView(this);
    }

    private void triggerAutoDismiss() {
        Scheduler.getUIHandler().removeCallbacks(this.mAutoDismissRunner);
        Scheduler.getUIHandler().postDelayed(this.mAutoDismissRunner, 8000);
    }

    private void clearAutoDismiss() {
        Scheduler.getUIHandler().removeCallbacks(this.mAutoDismissRunner);
    }

    public void setShowHideListener(OnShowHideListener<BaseMenuPopup> onShowHideListener) {
        this.mShowHideListener = onShowHideListener;
    }

    public boolean needPauseVideo() {
        return true;
    }

    public boolean dispatchTouchEvent(MotionEvent motionEvent) {
        triggerAutoDismiss();
        return super.dispatchTouchEvent(motionEvent);
    }
}
